using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WP.DB;
using WP.Commons;
using WP.Controls;
using WP.Modules ;
namespace WP.Modules
{
    public partial class frmUIUserStyle : BaseQryRpt 
    {
        private enum DtlColumn { Apply = 0, User, DefaultStyle }
        private string mstrProgName = "";
        private int miParameter = 0;
        public frmUIUserStyle()
        {
            InitializeComponent();
            InitGridDtl();
            this.mblnNeedSaveStartPositionInfo = false;
        }
        public void SetUserStyle(string strProgName,int iParameter)
        {
            mstrProgName = strProgName;
            miParameter = iParameter;
            FillStyle();
            this.ShowDialog(); 
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close(); 
        }
        private void FillStyle()
        {
            DBAccess dbcon = new DBAccess(false, false);
            DataView dvStyle = dbcon.GetDataTable("select distinct UiStyle from mFormDict where Progname<>'' and progname='" + mstrProgName  + "' and parameter=" + miParameter.ToString()   ).DefaultView;
            lvwStyle.Items.Clear();
            lvwStyle.View = View.LargeIcon;
            foreach (DataRowView drw in dvStyle)
            {
                ListViewItem ltem = new ListViewItem(drw["UiStyle"].ToString().Trim());
                ltem.ImageIndex = 0;
                lvwStyle.Items.Add(ltem);
            }
            dbcon.CloseCon();
        }
        private void InitGridDtl()
        {
            this.gdvDtl.Columns.Clear();
            this.gdvDtl.Rows.Clear();


            DataGridViewCheckBoxColumn colApply = new DataGridViewCheckBoxColumn();
            colApply.HeaderText = "Apply?";
            colApply.Width = 60;
            colApply.ReadOnly = false ;
            colApply.Name = "";
            DataGridViewTextBoxColumn colUserName = new DataGridViewTextBoxColumn();
            colUserName.HeaderText = "User";
            colUserName.Width = 100;
            colUserName.ReadOnly = true;
 
            DataGridViewCheckBoxColumn colDefaultStyle = new DataGridViewCheckBoxColumn();
            colDefaultStyle.HeaderText = "Default Style?";
            colDefaultStyle.Width = 60;
            colDefaultStyle.ReadOnly = false ;


            this.gdvDtl.Columns.AddRange(new DataGridViewColumn[] { colApply, colUserName, colDefaultStyle });
            this.gdvDtl.EditMode = DataGridViewEditMode.EditOnEnter ;
            this.gdvDtl.RowTemplate.Height = 20;
            this.gdvDtl.RowHeadersWidth = 23;
            this.gdvDtl.RowHeadersBorderStyle = DataGridViewHeaderBorderStyle.None;
            this.gdvDtl.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;
            this.gdvDtl.ColumnHeadersHeight = 20;
            this.gdvDtl.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
        }

        private void FillDtl()
        {
            this.gdvDtl.Rows.Clear();
            DBAccess dbcon = new DBAccess(false, false);
            DataView dvUser = dbcon.GetDataTable("select * from mUser order by username").DefaultView ;
            DataView dvUserStyle = dbcon.GetDataTable("select * from mUIUserStyle where UIStyle='" +  lvwStyle.FocusedItem.Text.Replace("'","''") + "' and  Progname='" + mstrProgName +"' and Parameter=" + miParameter.ToString()).DefaultView ;
            int i = 0;
            foreach (DataRowView drw in dvUser)
            {
                gdvDtl.Rows.Add();
                gdvDtl.Rows[i].Cells[(int)DtlColumn.User].Value = drw["UserName"].ToString().TrimEnd() ;
                dvUserStyle.RowFilter = "UserName='" + drw["UserName"].ToString() +"'"; ;
                if (dvUserStyle.Count > 0)
                {
                    gdvDtl.Rows[i].Cells[(int)DtlColumn.Apply].Value = true;
                    gdvDtl.Rows[i].Cells[(int)DtlColumn.DefaultStyle].Value = CvtFunc.ToBoolean(dvUserStyle[0]["Checked"]); 
                }
                else
                {
                    gdvDtl.Rows[i].Cells[(int)DtlColumn.Apply].Value = false ;
                    gdvDtl.Rows[i].Cells[(int)DtlColumn.DefaultStyle].Value = false;
                }
                i++;
            }
            dbcon.CloseCon();  
       }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            string strSql = "";
            DBAccess dbcon = new DBAccess(false, false);
            try
            {
               
                dbcon.BeginTran();
                for (int i = 0; i < gdvDtl.Rows.Count; i++)
                {
                    strSql = "Delete from mUIUserStyle where username='" + CvtFunc.ToString(gdvDtl.Rows[i].Cells[(int)DtlColumn.User].Value).Replace("'", "''") +
                              "' and UIStyle='" + lvwStyle.SelectedItems[0].Text.Replace("'", "''") + "' and Progname='" +
                              mstrProgName.Replace("'", "''") + "' and Parameter=" + miParameter.ToString();
                    dbcon.ExecuteSql(strSql);
                    if (CvtFunc.ToBoolean(gdvDtl.Rows[i].Cells[(int)DtlColumn.Apply].Value))
                    {
                        strSql = "Insert mUIUserStyle (UserName,UIStyle,Progname,Parameter,Checked) values ('" +
                                  CvtFunc.ToString(gdvDtl.Rows[i].Cells[(int)DtlColumn.User].Value).Replace("'", "''") + "','" +
                                  lvwStyle.SelectedItems[0].Text.Replace("'", "''") + "','" + mstrProgName.Replace("'", "''") + "'," +
                                  miParameter.ToString() + ","
                                  + (CvtFunc.ToBoolean(gdvDtl.Rows[i].Cells[(int)DtlColumn.DefaultStyle].Value) ? "1" : "0") + ")";
                        dbcon.ExecuteSql(strSql);

                        if (CvtFunc.ToBoolean(gdvDtl.Rows[i].Cells[(int)DtlColumn.DefaultStyle].Value))
                        {
                            strSql = "Update mUIUserStyle set Checked=0 where username='" + CvtFunc.ToString(gdvDtl.Rows[i].Cells[(int)DtlColumn.User].Value).Replace("'", "''") +
                                      "' and UIStyle<>'" + lvwStyle.SelectedItems[0].Text.Replace("'", "''") + "' and Progname='" +
                                      mstrProgName.Replace("'", "''") + "' and Parameter=" + miParameter.ToString();
                            dbcon.ExecuteSql(strSql);
                        }
                    }
                }

                dbcon.CommitTran();
                dbcon.CloseCon();
                this.Close();
            }
            catch (Exception x)
            {
                dbcon.RollbackTran();
                dbcon.CloseCon();  
                MsgBoxEx.Show(x.Message);
            }
        }

        private void lvwStyle_SelectedIndexChanged(object sender, EventArgs e)
        {
            FillDtl(); 
        }

        private void gdvDtl_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if(e.ColumnIndex ==(int) DtlColumn.DefaultStyle)
            {
                if (CvtFunc.ToBoolean(gdvDtl.Rows[e.RowIndex].Cells[e.ColumnIndex].Value))
                {
                    gdvDtl.Rows[e.RowIndex].Cells[(int)DtlColumn.Apply].Value = true;
                }
            }
        }

        private void gdvDtl_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
           
        }
    }
}